package com.yy.show.util;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import com.yy.base.util.DateUtil;
import com.yy.base.util.SessionInfo;
import com.yy.module.entity.ModuleInfo;

/**
 * 模块处理类
 */
@SuppressWarnings("unchecked")
public abstract class ModuleHandle {
	public static String UU_ID = "${uuid}";//登录用户ID
	public static String USER_ID = "${userId}";//登录用户ID
	public static String USER_CODE = "${userCode}";//登录用账号
	public static String USER_ORG_ID = "${OrgId}";//登录用机构ID
	public static String SYS_DATE = "${sysDate}";//当前系统时间
	public static String SYS_YEAR = "${sysYear}";//当前系统月份
	public static String SYS_MONTH = "${sysMonth}";//当前系统月份
	public static String SYS_YEAR_MONTH = "${sysYearMonth}";//当前系统年月
	
	protected ModuleInfo module = null;
	protected Map<String, String> tables = null;
	protected List<Map> onlyField = null;
	protected Map values = null;
	protected String error = null;
	protected StringBuilder sql = null;
	private String id;//生成的主键uuid
	private String keyColumn;//主键字段
	protected HttpServletRequest request;
	
	public ModuleHandle(ModuleInfo module, HttpServletRequest request) {
		this.module = module;
		this.request = request;
	}
	/**
	 * 根据设置默认值
	 */
	protected Object getDefaultValue(String defaultValue) {
		if (defaultValue != null) {
			if (UU_ID.equals(defaultValue)) {
				return UUID.randomUUID().toString().replaceAll("-", "");
			} else if (USER_ID.equals(defaultValue)) {
				return SessionInfo.getUser().getId();
			} else if (USER_CODE.equals(defaultValue)) {
				return SessionInfo.getUser().getUserName();
			} else if (USER_ORG_ID.equals(defaultValue)) {
				return SessionInfo.getUser().getOrgId();
			} else if (SYS_DATE.equals(defaultValue)) {
				return new Date();
			} else if (SYS_YEAR.equals(defaultValue)) {
				return DateUtil.getYear();
			} else if (SYS_MONTH.equals(defaultValue)) {
				return DateUtil.getMonth();
			} else if (SYS_YEAR_MONTH.equals(defaultValue)) {
				int year = DateUtil.getYear();
				int month = DateUtil.getMonth();
				return month < 10 ? (year + "0" + month) : (year + "" + month);
			}
		}
		return defaultValue;
	}
	public Map getParamMap() {
		return values;
	}
	public void setError(String error) {
		this.error = error;
	}
	public String getError() {
		return error;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getKeyColumn() {
		return keyColumn;
	}
	public void setKeyColumn(String keyColumn) {
		this.keyColumn = keyColumn;
	}
	public List<Map> getOnlyField() {
		return onlyField;
	}
	public void setOnlyField(List<Map> onlyField) {
		this.onlyField = onlyField;
	}
}